package org.hot100_6.leetcode3;

import java.util.HashSet;

public class Solution {

    public static void main(String[] args) {
        String str = "abcabcbb";
        int length = lengthOfLongestSubstring(str);
        System.out.println(length);
    }
    public static int lengthOfLongestSubstring(String s) {
        int n = s.length();
        if (s==null || n==0){
            return n;
        }
        HashSet<Character> hashSet = new HashSet<>();
        int left = 0;
        int maxLength = 1;
        for (int right = 0; right < n; right++) {
            while (left<=right && hashSet.contains(s.charAt(right))){
                hashSet.remove(s.charAt(left));
                left++;
            }
            hashSet.add(s.charAt(right));
            maxLength=Math.max(maxLength,right-left);
        }

        return maxLength;
    }
}
